

package com.maoshi.shop.distribution.platform.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.maoshi.shop.common.config.Constant;
import com.maoshi.shop.common.util.PageParam;
import com.maoshi.shop.distribution.comment.dto.DistributionUserAchievementDataDto;
import com.maoshi.shop.distribution.comment.model.DistributionUser;
import com.maoshi.shop.distribution.comment.param.RangeTimeParam;
import com.maoshi.shop.distribution.comment.param.UpdateDistributionUserParam;
import com.maoshi.shop.distribution.comment.service.DistributionUserService;
import com.maoshi.shop.security.platform.util.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @author lgh on 2019/04/01.
 */
@RestController
@RequestMapping("/distribution/distributionUser")
public class DistributionUserController {

    @Autowired
    private DistributionUserService distributionUserService;

    /**
     * 分页获取
     */
    @GetMapping("/page")
    @PreAuthorize("@pms.hasPermission('distribution:distributionUser:page')")
    public ResponseEntity<IPage<DistributionUser>> page(DistributionUser distributionUser, RangeTimeParam rangeTimeParam, String userMobile, String parentMobile, PageParam<DistributionUser> page) {
        IPage<DistributionUser> list = distributionUserService.distributionUserPage(page, distributionUser, rangeTimeParam, userMobile, parentMobile);
        return ResponseEntity.ok(list);
    }


    /**
     * 分销员各项数据信息
     */
    @GetMapping("/info/achievementData/{id}")
    @PreAuthorize("@pms.hasPermission('distribution:distributionUser:info')")
    public ResponseEntity<DistributionUserAchievementDataDto> getDistributionUserAchievementData(@PathVariable("id") Long id) {
        return ResponseEntity.ok(distributionUserService.getDistributionUserLevelAchievementDataByDistributionUserId(id));
    }


    /**
     * 获取信息
     */
    @GetMapping("/info/")
    @PreAuthorize("@pms.hasPermission('distribution:distributionUser:info')")
    public ResponseEntity<DistributionUser> info(@PathVariable("id") Long id) {
        DistributionUser distributionUser = distributionUserService.getById(id);
        return ResponseEntity.ok(distributionUser);
    }


    /**
     * 获取业绩
     */
    @GetMapping("/page/achievement")
    @PreAuthorize("@pms.hasPermission('distribution:distributionUser:page')")
    public ResponseEntity<IPage<DistributionUser>> page(DistributionUser distributionUser, String userMobile, PageParam<DistributionUser> page) {
        distributionUser.setShopId(Constant.PLATFORM_SHOP_ID);
        IPage<DistributionUser> list = distributionUserService.getDistributionUserAchievementPage(page, distributionUser, userMobile);
        return ResponseEntity.ok(list);
    }


    /**
     * 保存
     */
    @PostMapping
    @PreAuthorize("@pms.hasPermission('distribution:distributionUser:save')")
    public ResponseEntity<Void> save(@RequestBody @Valid DistributionUser distributionUser) {
        distributionUserService.save(distributionUser);
        return ResponseEntity.ok().build();
    }

    /**
     * 修改
     */
    @PutMapping
    @PreAuthorize("@pms.hasPermission('distribution:distributionUser:update')")
    public ResponseEntity<Void> update(@RequestBody @Valid DistributionUser distributionUser) {
        distributionUserService.updateById(distributionUser);
        return ResponseEntity.ok().build();
    }

    /**
     * 修改分销员状态
     */
    @PutMapping("/state")
    @PreAuthorize("@pms.hasPermission('distribution:distributionUser:update')")
    public ResponseEntity<Void> update(@RequestBody UpdateDistributionUserParam param) {

        distributionUserService.updateSelectiveAndInsertDistributionUserBan(param, SecurityUtils.getSysUser().getUserId());
        DistributionUser dbDistributionUser = distributionUserService.getById(param.getDistributionUserId());
        distributionUserService.removeCacheByUserIdAndShopId(dbDistributionUser.getUserId(), dbDistributionUser.getShopId());
        distributionUserService.removeCacheByCardNo(dbDistributionUser.getCardNo());
        return ResponseEntity.ok().build();
    }

    /**
     * 删除
     */
    @DeleteMapping
    @PreAuthorize("@pms.hasPermission('distribution:distributionUser:delete')")
    public ResponseEntity<Void> delete(@RequestBody List<Long> ids) {
        distributionUserService.removeByIds(ids);
        return ResponseEntity.ok().build();
    }

}
